package cn.wolfcode.web.advice;

import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.qo.JsonResult;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 异常类处理
 */
@ControllerAdvice
public class ExceptionControllerAdvice {

    //    //自定义一个异常类.  想给用户看到的信息
//    @ExceptionHandler(BusinessException.class)
//    public String handleException(RuntimeException e, HandlerMethod method, HttpServletResponse resp, Model model) {
//        //异常类分为Ajax请求出现异常和普通页面出现异常
//        if (method.hasMethodAnnotation(ResponseBody.class)) {
//            resp.setContentType("application/json;charset=utf-8");
//            try {
//                resp.getWriter().write(JSON.toJSONString(new JsonResult(false, e.getMessage())));
//            } catch (IOException ioException) {
//                ioException.printStackTrace();
//            }
//            return null;
//        } else {
//            //普通页面请求异常
//            model.addAttribute("errorMsg", e.getMessage());
//            return "common/error";
//        }
//
//    }
    //自定义一个异常类,  想给用户看到的信息
    @ExceptionHandler(BusinessException.class)
    public String handlerException(RuntimeException e, HandlerMethod method, HttpServletResponse resp, Model model) {
        //异常类分类
        if (method.hasMethodAnnotation(ResponseBody.class)) {
            resp.setContentType("application/json;charset=utf-8");
            try {
                resp.getWriter().write(JSON.toJSONString(new JsonResult(false, e.getMessage())));
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            return null;
        }else {
            //普通页面异常
            model.addAttribute("errorMsg", e.getMessage());
            return "common/error";
        }
    }

    //自定义异常处理.  想给用户看到的信息
    @ExceptionHandler(Exception.class)
    public String handleException(Exception e, HandlerMethod method, HttpServletResponse resp, Model model) {

        //异常类分为Ajax请求出现异常和普通页面出现异常
        if (method.hasMethodAnnotation(ResponseBody.class)) {
            resp.setContentType("application/json;charset=utf-8");
            try {
                e.printStackTrace();
                resp.getWriter().write(JSON.toJSONString(new JsonResult(false, "服务器崩溃~~请联系管理员")));
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            return null;
        } else {
            //普通页面请求异常
            e.printStackTrace();
            model.addAttribute("errorMsg", "服务器崩溃~~请联系管理员");
            return "common/error";  //自动添加前后缀
        }

    }

    //自定义异常处理.  想给用户看到的信息
    @ExceptionHandler(AuthorizationException.class)
    public String handleException(AuthorizationException e, HandlerMethod method, HttpServletResponse resp, Model model) {
        //异常类分为Ajax请求出现异常和普通页面出现异常
        if (method.hasMethodAnnotation(ResponseBody.class)) {
            resp.setContentType("application/json;charset=utf-8");
            try {
                e.printStackTrace();
                resp.getWriter().write(JSON.toJSONString(new JsonResult(false, "你还没有权限访问")));
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            return null;
        } else {
            //普通页面请求异常
            model.addAttribute("errorMsg", "你还没有权限访问");
            return "redirect:/permission/nopermission";  //自动添加前后缀
        }

    }

}
